package com.woniuxy.main.pay.handler;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.main.mycenter_ty.pojo.User_Ty;
import com.woniuxy.main.mycenter_ty.service.MyCenterService;
import com.woniuxy.main.pay.pojo.Order;
import com.woniuxy.main.pay.pojo.OrderTypeCount;
import com.woniuxy.main.pay.service.OrderService;
import com.woniuxy.main.pay.service.PayService;

@Controller
public class PayHandler {
	@Autowired
	private PayService payService;
	@Autowired
	private MyCenterService personCenterservice;
	
	// private Integer userid=0;
	
	/**
	 *  进入我的钱包页面
	 * @param userId
	 * @return
	 */
	@RequestMapping("/myWallet")
	@ResponseBody
    public ModelAndView myWallet(){
		String phone = (String) SecurityUtils.getSubject().getPrincipal();
		User_Ty user = personCenterservice.queryAllInfoByPhone(phone); // 个人资料
		Integer userid=user.getUser_id();   // 当前用户的id
		Integer balance;
		balance=payService.searchBalance(userid);
		ModelAndView ma=new ModelAndView();
		ma.addObject("userId", userid);
		ma.addObject("balance", balance);
		ma.setViewName("/wallet.html");
		return ma;
    }
	
	
	
	@RequestMapping("/substract")
	@ResponseBody
	public ModelAndView substract(Integer userId,Integer moneySum){
		Integer balance=payService.searchBalance(userId);
		Integer money=balance-moneySum;
		payService.addBalance(userId,money);
		ModelAndView ma=new ModelAndView();
		ma.addObject("userId", userId);
		ma.addObject("balance", balance);
		ma.setViewName("/wallet.html");
		return ma;
	}
	
	@RequestMapping("/pa")
	@ResponseBody
	public Map pa(Integer userId,Integer moneySum,HttpServletRequest request){
		Map<String, Integer> map=new HashMap<String, Integer>();
		HttpSession session=request.getSession();
		session.setAttribute("userID", userId);
		session.setAttribute("moneySum", moneySum);
		map.put("userId", userId);
		map.put("moneySum", moneySum);
		return map;
	}
	
	
	
}
